﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace ZYCommon
{
    

    public class PrintPageSettings : DevComponents.DotNetBar.Office2007Form
    {
        private Button btnCancel;
        private Button btnOK;
        private Container components = null;
        private GroupBox groupBox1;
        private GroupBox groupBox2;
        private GroupBox groupBox3;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private Label label6;
        private System.Drawing.Printing.PageSettings myPageSettings = new System.Drawing.Printing.PageSettings();
        private bool pixelUnit = true;
        private RadioButton rdBtnCM;
        private RadioButton rdBtnPixel;
        private TextBox txtBottom;
        private TextBox txtHeight;
        private TextBox txtLeft;
        private TextBox txtRight;
        private TextBox txtTop;
        private TextBox txtWidth;

        public PrintPageSettings()
        {
            this.InitializeComponent();
            this.myPageSettings.PaperSize = new PaperSize("Custom", 0x31a, 0x463);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!this.IsValidated())
            {
                this.ShowInvalidatedParamDialog();
                base.DialogResult = DialogResult.None;
            }
            else if (this.rdBtnPixel.Checked)
            {
                this.myPageSettings.PaperSize.Width = Convert.ToInt32(this.txtWidth.Text);
                this.myPageSettings.PaperSize.Height = Convert.ToInt32(this.txtHeight.Text);
                this.myPageSettings.Margins.Top = Convert.ToInt32(this.txtTop.Text);
                this.myPageSettings.Margins.Bottom = Convert.ToInt32(this.txtBottom.Text);
                this.myPageSettings.Margins.Left = Convert.ToInt32(this.txtLeft.Text);
                this.myPageSettings.Margins.Right = Convert.ToInt32(this.txtRight.Text);
            }
            else
            {
                this.myPageSettings.PaperSize.Width = this.CMToP(Convert.ToSingle(this.txtWidth.Text));
                this.myPageSettings.PaperSize.Height = this.CMToP(Convert.ToSingle(this.txtHeight.Text));
                this.myPageSettings.Margins.Top = this.CMToP(Convert.ToSingle(this.txtTop.Text));
                this.myPageSettings.Margins.Bottom = this.CMToP(Convert.ToSingle(this.txtBottom.Text));
                this.myPageSettings.Margins.Left = this.CMToP(Convert.ToSingle(this.txtLeft.Text));
                this.myPageSettings.Margins.Right = this.CMToP(Convert.ToSingle(this.txtRight.Text));
            }
        }

        private void CMToP()
        {
            this.txtWidth.Text = this.CMToP(Convert.ToSingle(this.txtWidth.Text)).ToString();
            this.txtHeight.Text = this.CMToP(Convert.ToSingle(this.txtHeight.Text)).ToString();
            this.txtTop.Text = this.CMToP(Convert.ToSingle(this.txtTop.Text)).ToString();
            this.txtBottom.Text = this.CMToP(Convert.ToSingle(this.txtBottom.Text)).ToString();
            this.txtLeft.Text = this.CMToP(Convert.ToSingle(this.txtLeft.Text)).ToString();
            this.txtRight.Text = this.CMToP(Convert.ToSingle(this.txtRight.Text)).ToString();
        }

        private int CMToP(float fValue)
        {
            return (int) ((fValue * 37.8f) + 0.5);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.btnOK = new Button();
            this.btnCancel = new Button();
            this.groupBox1 = new GroupBox();
            this.rdBtnCM = new RadioButton();
            this.rdBtnPixel = new RadioButton();
            this.groupBox2 = new GroupBox();
            this.txtHeight = new TextBox();
            this.label2 = new Label();
            this.txtWidth = new TextBox();
            this.label1 = new Label();
            this.groupBox3 = new GroupBox();
            this.txtRight = new TextBox();
            this.label5 = new Label();
            this.txtLeft = new TextBox();
            this.label6 = new Label();
            this.txtBottom = new TextBox();
            this.label3 = new Label();
            this.txtTop = new TextBox();
            this.label4 = new Label();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            base.SuspendLayout();
            this.btnOK.DialogResult = DialogResult.OK;
            this.btnOK.Location = new Point(0x5b, 0x120);
            this.btnOK.Name = "btnOK";
            this.btnOK.TabIndex = 3;
            this.btnOK.Text = "确定";
            this.btnOK.Click += new EventHandler(this.btnOK_Click);
            this.btnCancel.DialogResult = DialogResult.Cancel;
            this.btnCancel.Location = new Point(0xe3, 0x120);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.TabIndex = 4;
            this.btnCancel.Text = "取消";
            this.groupBox1.Controls.Add(this.rdBtnCM);
            this.groupBox1.Controls.Add(this.rdBtnPixel);
            this.groupBox1.Location = new Point(0x10, 0x10);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new Size(360, 0x38);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "单位";
            this.rdBtnCM.Location = new Point(200, 0x18);
            this.rdBtnCM.Name = "rdBtnCM";
            this.rdBtnCM.TabIndex = 1;
            this.rdBtnCM.Text = "厘米(cm)";
            this.rdBtnCM.Click += new EventHandler(this.rdBtnCM_Click);
            this.rdBtnPixel.Checked = true;
            this.rdBtnPixel.Location = new Point(0x38, 0x18);
            this.rdBtnPixel.Name = "rdBtnPixel";
            this.rdBtnPixel.TabIndex = 0;
            this.rdBtnPixel.TabStop = true;
            this.rdBtnPixel.Text = "像素(Pixel)";
            this.rdBtnPixel.Click += new EventHandler(this.rdBtnPixel_Click);
            this.groupBox2.Controls.Add(this.txtHeight);
            this.groupBox2.Controls.Add(this.label2);
            this.groupBox2.Controls.Add(this.txtWidth);
            this.groupBox2.Controls.Add(this.label1);
            this.groupBox2.Location = new Point(0x10, 0x58);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new Size(360, 0x40);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "页面大小";
            this.txtHeight.Location = new Point(0xf2, 0x18);
            this.txtHeight.Name = "txtHeight";
            this.txtHeight.TabIndex = 3;
            this.txtHeight.Text = "";
            this.label2.Location = new Point(0xba, 0x18);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x36, 0x17);
            this.label2.TabIndex = 2;
            this.label2.Text = "高度(H):";
            this.txtWidth.Location = new Point(0x4a, 0x18);
            this.txtWidth.Name = "txtWidth";
            this.txtWidth.TabIndex = 1;
            this.txtWidth.Text = "";
            this.label1.Location = new Point(0x12, 0x18);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x36, 0x17);
            this.label1.TabIndex = 0;
            this.label1.Text = "宽度(W):";
            this.groupBox3.Controls.Add(this.txtRight);
            this.groupBox3.Controls.Add(this.label5);
            this.groupBox3.Controls.Add(this.txtLeft);
            this.groupBox3.Controls.Add(this.label6);
            this.groupBox3.Controls.Add(this.txtBottom);
            this.groupBox3.Controls.Add(this.label3);
            this.groupBox3.Controls.Add(this.txtTop);
            this.groupBox3.Controls.Add(this.label4);
            this.groupBox3.Location = new Point(0x10, 0xa8);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new Size(360, 0x68);
            this.groupBox3.TabIndex = 2;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "页边距";
            this.txtRight.Location = new Point(0xf2, 0x40);
            this.txtRight.Name = "txtRight";
            this.txtRight.TabIndex = 11;
            this.txtRight.Text = "";
            this.label5.Location = new Point(0xba, 0x40);
            this.label5.Name = "label5";
            this.label5.Size = new Size(0x30, 0x17);
            this.label5.TabIndex = 10;
            this.label5.Text = "右(R):";
            this.txtLeft.Location = new Point(0x4a, 0x40);
            this.txtLeft.Name = "txtLeft";
            this.txtLeft.TabIndex = 9;
            this.txtLeft.Text = "";
            this.label6.Location = new Point(0x12, 0x40);
            this.label6.Name = "label6";
            this.label6.Size = new Size(0x30, 0x17);
            this.label6.TabIndex = 8;
            this.label6.Text = "左(L):";
            this.txtBottom.Location = new Point(0xf2, 0x18);
            this.txtBottom.Name = "txtBottom";
            this.txtBottom.TabIndex = 7;
            this.txtBottom.Text = "";
            this.label3.Location = new Point(0xba, 0x18);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0x30, 0x17);
            this.label3.TabIndex = 6;
            this.label3.Text = "下(B):";
            this.txtTop.Location = new Point(0x4a, 0x18);
            this.txtTop.Name = "txtTop";
            this.txtTop.TabIndex = 5;
            this.txtTop.Text = "";
            this.label4.Location = new Point(0x12, 0x18);
            this.label4.Name = "label4";
            this.label4.Size = new Size(0x30, 0x17);
            this.label4.TabIndex = 4;
            this.label4.Text = "上(T):";
            this.AutoScaleBaseSize = new Size(6, 14);
            base.ClientSize = new Size(0x188, 0x146);
            base.Controls.Add(this.groupBox3);
            base.Controls.Add(this.groupBox2);
            base.Controls.Add(this.groupBox1);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnOK);
            base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "PrintPageSettings";
            base.StartPosition = FormStartPosition.CenterParent;
            this.Text = "页面设置";
            base.Load += new EventHandler(this.PrintPageSettings_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.groupBox3.ResumeLayout(false);
            base.ResumeLayout(false);
        }

        private bool IsValidated()
        {
            bool flag = false;
            flag = StringCommon.IsNumberic(this.txtWidth.Text) & StringCommon.IsNumberic(this.txtHeight.Text);
            flag &= StringCommon.IsNumberic(this.txtTop.Text);
            flag &= StringCommon.IsNumberic(this.txtBottom.Text);
            flag &= StringCommon.IsNumberic(this.txtLeft.Text);
            return (flag & StringCommon.IsNumberic(this.txtRight.Text));
        }

        private void PrintPageSettings_Load(object sender, EventArgs e)
        {
            this.txtWidth.Text = this.myPageSettings.PaperSize.Width.ToString();
            this.txtHeight.Text = this.myPageSettings.PaperSize.Height.ToString();
            this.txtTop.Text = this.myPageSettings.Margins.Top.ToString();
            this.txtBottom.Text = this.myPageSettings.Margins.Bottom.ToString();
            this.txtLeft.Text = this.myPageSettings.Margins.Left.ToString();
            this.txtRight.Text = this.myPageSettings.Margins.Right.ToString();
        }

        private void PToCM()
        {
            this.txtWidth.Text = this.PToCM(Convert.ToInt32(this.txtWidth.Text)).ToString(".00");
            this.txtHeight.Text = this.PToCM(Convert.ToInt32(this.txtHeight.Text)).ToString(".00");
            this.txtTop.Text = this.PToCM(Convert.ToInt32(this.txtTop.Text)).ToString(".00");
            this.txtBottom.Text = this.PToCM(Convert.ToInt32(this.txtBottom.Text)).ToString(".00");
            this.txtLeft.Text = this.PToCM(Convert.ToInt32(this.txtLeft.Text)).ToString(".00");
            this.txtRight.Text = this.PToCM(Convert.ToInt32(this.txtRight.Text)).ToString(".00");
        }

        private float PToCM(int iValue)
        {
            float num = iValue;
            return (num / 37.8f);
        }

        private void rdBtnCM_Click(object sender, EventArgs e)
        {
            if (!this.IsValidated())
            {
                if (this.pixelUnit)
                {
                    this.ShowInvalidatedParamDialog();
                    this.rdBtnPixel.Checked = true;
                }
            }
            else if (this.pixelUnit)
            {
                this.pixelUnit = false;
                this.PToCM();
            }
        }

        private void rdBtnPixel_Click(object sender, EventArgs e)
        {
            if (!this.IsValidated())
            {
                if (!this.pixelUnit)
                {
                    this.ShowInvalidatedParamDialog();
                    this.rdBtnCM.Checked = true;
                }
            }
            else if (!this.pixelUnit)
            {
                this.pixelUnit = true;
                this.CMToP();
            }
        }

        private void ShowInvalidatedParamDialog()
        {
            MessageBox.Show("输入的参数无效，请确保输入的都是数值！");
        }

        private void UpdateData(bool bGet)
        {
            if (!bGet)
            {
            }
        }

        public System.Drawing.Printing.PageSettings PageSettings
        {
            get
            {
                return this.myPageSettings;
            }
            set
            {
                this.myPageSettings = value;
            }
        }
    }
}

